annual <- read.dta(paste0(datadir, "/",
                               "indonesia_annual.dta"))

annual <- annual[annual$adm_year>=2005,]

categories <- c('other',
                'police',
                'popjustice',
                'resource',
                'governance',
                'election',
                'identity',
                'separatist')

annual$running.sum <- 0
stacked_list <- c()
for (c in categories) {
  annual$running.sum <- (annual$running.sum
                              + annual[, paste0('incident',c,
                                                     '_year05')])
  annual[[paste0(c,
                       '_stacked')]] <- annual$running.sum
  stacked_list <- c(paste0(c,
                           '_stacked'),
                    stacked_list)
}

annual$zero = 0
stacked_list <- c(stacked_list,
                  'zero')

denskey <- c(separatist_stacked = NA,
            identity_stacked = NA,
            election_stacked = 15,
            governance_stacked = 15,
            resource_stacked = NA,
            popjustice_stacked = 20,
            police_stacked = NA,
            other_stacked = 10)
colors <- c(separatist_stacked = 'black',
            identity_stacked = 'gray40',
            election_stacked = 'black',
            governance_stacked = 'black',
            resource_stacked = 'gray88',
            popjustice_stacked = 'gray',
            police_stacked = 'white',
            other_stacked = 'black')
angles <- c(separatist_stacked = NA,
               identity_stacked = NA,
               election_stacked = 45,
               governance_stacked = -45,
               resource_stacked = NA,
               popjustice_stacked = 3,
               police_stacked = NA,
               other_stacked = 90)

pdf(paste("graphs/indo_by_categories.pdf",
          sep=""),
    width = 7, 
    height = 7)

par(mar=c(7, 4, 2, 2) + 0.1)
plot(annual$adm_year,
     annual[,
                 stacked_list[1]],
     xlim = c(2005, 2014),
     ylim = c(0, 5000),
     type = "l",
     xlab = "",
     ylab = "Violent Incidents")
for (i in 2:length(stacked_list)) {
  lines(annual$adm_year,
        annual[,
                    stacked_list[i]],
        xlab = "",
        ylab = "Violent Incidents")
  polygon(c(annual$adm_year, rev(annual$adm_year)), 
          c(annual[,
                   stacked_list[i]] ,
            rev(annual[,
                    stacked_list[i-1]])),
          density = denskey[stacked_list[i-1]],
          col = colors[stacked_list[i-1]],
          border = 'black',
          angle = angles[stacked_list[i-1]])
} 
par(xpd=TRUE)

labels = c(separatist_stacked = "Separatist",
           identity_stacked = "Identity",
           election_stacked = "Election",
           governance_stacked = "Governance",
           resource_stacked = "Resource",
           popjustice_stacked = "Popular Justice",
           police_stacked = "Police",
           other_stacked = "Other")
legend(x = 2010, y = -800,
       legend = labels[names(denskey)],
       ncol = 3,
       fill = colors,
       density = denskey,
       angle = angles,
       xjust = .5,
       cex = .8)
dev.off()

